package com.guobox.movies.view.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.guobox.movies.APP;
import com.guobox.movies.R;
import com.guobox.movies.entity.MovieEntity;
import com.guobox.movies.proxy.HttpUrls;

import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by xuxinlei on 2017/6/30.
 */

public class Radapter extends RecyclerView.Adapter<Radapter.MviewHolder> {

    private List<MovieEntity> movieEntities;
    private String currentType = "";
    private Context context;
    private static final int TYPE_HEADER = 0;
    private static final int TYPE_NORMAL = 1;
    private View mHeaderView;

    public Radapter(List<MovieEntity> movieEntities, String currentType, Context context) {
        this.movieEntities = movieEntities;
        this.currentType = currentType;
        this.context = context;
    }

    public void setMovieEntities(List<MovieEntity> movieEntities) {
        this.movieEntities = movieEntities;
    }

    public void setCurrentType(String currentType) {
        this.currentType = currentType;
    }

    public void setHeaderView(View headerView) {
        mHeaderView = headerView;
        notifyItemInserted(0);
    }

    @Override
    public int getItemViewType(int position) {
        if (mHeaderView == null) return TYPE_NORMAL;
        if (position == 0) return TYPE_HEADER;
        return TYPE_NORMAL;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager) {
            final GridLayoutManager gridManager = ((GridLayoutManager) manager);
            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    return getItemViewType(position) == TYPE_HEADER
                            ? gridManager.getSpanCount() : 1;
                }
            });
        }
    }

    @Override
    public MviewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mHeaderView != null && viewType == TYPE_HEADER)
            return new MviewHolder(mHeaderView);
        return new MviewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_t_movielist, null));
    }


    @Override
    public void onBindViewHolder(MviewHolder holder, int position) {
        if (getItemViewType(position) == TYPE_HEADER) {
            holder.tHeaderText.setText(currentType);
            return;
        }
        final int pos = getRealPosition(holder);
        holder.setIsRecyclable(false);
        if (movieEntities.get(pos).getMetadata() != null) {
            Glide.with(context)
                    .load(HttpUrls.getMoviePosterURL(
                            movieEntities.get(pos).getMetadata().getMovieId()
                            , HttpUrls.Pic_Middle)
                            .build().toString())
                    .override(305, 406)
                    .centerCrop()
                    .into(holder.imageView);
            if (holder.movieNameTips.getVisibility() != View.GONE) {
                holder.movieNameTips.setVisibility(View.GONE);
                holder.movieNameTips.setText("");
            }
            if (movieEntities.get(pos).getType().equals("series")) {
                if (holder.movieSerial.getVisibility() != View.VISIBLE)
                    holder.movieSerial.setVisibility(View.VISIBLE);
            } else {
                if (holder.movieSerial.getVisibility() != View.INVISIBLE)
                    holder.movieSerial.setVisibility(View.INVISIBLE);
            }
            if (movieEntities.get(pos).getDimension().equals("3D")) {
                if (holder.movie3d.getVisibility() != View.VISIBLE)
                    holder.movie3d.setVisibility(View.VISIBLE);
            } else {
                if (holder.movie3d.getVisibility() != View.GONE)
                    holder.movie3d.setVisibility(View.GONE);
            }
            if (Objects.equals(movieEntities.get(pos).getUhd(),"4k")) {
                if (holder.movie4K.getVisibility() != View.VISIBLE)
                    holder.movie4K.setVisibility(View.VISIBLE);
            } else {
                if (holder.movie4K.getVisibility() != View.GONE)
                    holder.movie4K.setVisibility(View.GONE);
            }
        } else {
            if (holder.movieSerial.getVisibility() != View.INVISIBLE)
                holder.movieSerial.setVisibility(View.INVISIBLE);
            if (holder.movie3d.getVisibility() == View.VISIBLE)
                holder.movie3d.setVisibility(View.GONE);
            if (holder.movie4K.getVisibility() == View.VISIBLE)
                holder.movie4K.setVisibility(View.GONE);
            if (holder.movieNameTips.getVisibility() != View.VISIBLE)
                holder.movieNameTips.setVisibility(View.VISIBLE);
            Glide.with(context).load(R.drawable.poster_default)
                    .override(305, 406)
                    .centerCrop()
                    .into(holder.imageView);
            holder.movieNameTips.setText(movieEntities.get(pos).getName());
        }
        holder.itemView.setTag(pos);
        if (movieEntities.get(pos).getMetadata() != null) {
            holder.tMovieName.setText(movieEntities.get(pos).getMetadata().getMovieName());
        } else {
            holder.tMovieName.setText(movieEntities.get(pos).getName());
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, movieEntities.get(pos).getName(), Toast.LENGTH_SHORT).show();

            }
        });
    }

    @Override
    public int getItemCount() {
        return mHeaderView == null ? movieEntities.size() : movieEntities.size() + 1;
    }

    private int getRealPosition(RecyclerView.ViewHolder holder) {
        int position = holder.getLayoutPosition();
        return mHeaderView == null ? position : position - 1;
    }

    public boolean isContain(String str, String pattern) {

        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(str);
        return m.find();
    }

    class MviewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView tMovieName;
        TextView tHeaderText;
        TextView movieNameTips;
        ImageView movieSerial;
        ImageView movie3d;
        ImageView movie4K;

        MviewHolder(View itemView) {
            super(itemView);
            itemView.setFocusable(false);
            if (itemView == mHeaderView) {
                tHeaderText = (TextView) mHeaderView.findViewById(R.id.tv_header_movielist);
                tHeaderText.setTypeface(APP.getTypeface());
                return;
            }
            imageView = (ImageView) itemView.findViewById(R.id.iv_test);
            tMovieName = (TextView) itemView.findViewById(R.id.tv_t_movie_name);
            movieNameTips = (TextView) itemView.findViewById(R.id.tv_movie_name_tips);
            movieSerial = (ImageView) itemView.findViewById(R.id.iv_serial);
            movie3d = (ImageView) itemView.findViewById(R.id.iv_dimension);
            movie4K = itemView.findViewById(R.id.iv_4k);
            tMovieName.setTypeface(APP.getTypeface());
            movieNameTips.setTypeface(APP.getTypeface());
        }
    }
}